<template>
	<view class="container">
		<view class="form">
			<uni-forms ref="formRef" :model="user" :rules="rules" label-width="80px">
				<uni-forms-item label="新密码" prop="newPassword">
					<uni-easyinput v-model="user.newPassword" placeholder="请输入新密码" type="password" show-password />
				</uni-forms-item>
				<uni-forms-item>
					<button class="mini-btn xy-btn" type="primary" size="mini" @click="submit()">保存</button>
					<button class="mini-btn xy-btn" type="warn" size="mini" @click="close()">关闭</button>
				</uni-forms-item>
			</uni-forms>
		</view>
	</view>
</template>

<script setup>
	import {
		resetUserPwd
	} from "@/pages_work/api/system/user";
	import {
		ref,
		getCurrentInstance,
		reactive
	} from 'vue'
	import {
		onLoad,
		onUnload
	} from '@dcloudio/uni-app'

	const {
		proxy
	} = getCurrentInstance();

	const user = reactive({
		newPassword: undefined,
	});

	const rules = ref(proxy.uniappRules({
		newPassword: [{
				required: true,
				message: "密码不能为空",
				trigger: "blur"
			}, {
				min: 5,
				max: 20,
				message: "密码长度必须介于 5 和 20 之间",
				trigger: "blur"
			},
			{
				pattern: /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&#.])[A-Za-z\d@$!%*?&#.]{5,20}$/,
				message: "至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符",
				trigger: "blur"
			}
		],
	}))


	onLoad((params) => {
		uni.setNavigationBarTitle({
			title: params.title
		});
		user.userId = params.userId
	})
	/** 提交按钮 */
	function submit() {
		proxy.$refs["formRef"].validate().then(valid => {
			if (valid) {
				resetUserPwd(user.userId, user.newPassword).then(response => {
					proxy.$modal.msgSuccess("修改成功");
				});
			}
		});
	};

	/** 关闭按钮 */
	function close() {
		uni.navigateBack()
	};
</script>

<style lang="scss">
	@import "@/static/scss/work/form.scss";
</style>